//
//  HelpDetailViewController.swift
//  weile
//
//  Created by LKM on 16/12/13.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit

//拷贝自UserProtocolViewController 帮助详情也是文章内容来的
class HelpDetailViewController: BaseViewController {
    
    var id:String!;
    
    var data:ArticleDetailTo?
    
    var webView :UIWebView!;
    override func viewDidLoad() {
        super.viewDidLoad()
        isNavigationBarOpacitas=true;
        
        webView = UIWebView()
        webView.frame = view.bounds
        /// 自动对页面进行缩放以适应屏幕
        webView.scalesPageToFit = true
        webView.dataDetectorTypes = .all
        webView.backgroundColor = UIColor.clear
        webView.isOpaque = false;
        
        webView.dataDetectorTypes = UIDataDetectorTypes.link//.remove(UIDataDetectorTypes.link);
        //        LKMLog("=====\(Bundle.main.path(forResource: "html/productDetail", ofType: "html")!)")
        //        let url = URL(string:Bundle.main.path(forResource: "html/productDetail", ofType: "html")! )
        //        let request = URLRequest(url: url!)
        //        webView.loadRequest(request)
        
        
        webView.delegate = self
        view.addSubview(webView)
        
        
        loadData()
        
    }
    
    func loadData(){
        ViewHelp.showProgressActivity(containView: self.view)
        let ts=Api.getHelpDetail(preTaskSerialNumber: nil,id:id) { (re:ResponEntity) in
            
            if(re.isSuccess()){
                
                DispatchQueue.global(qos: .background).async(execute: {
                    
                    let data=re.getData() as! ArticleDetailTo;
                    self.data=data;
                    var xx="";
                    do {
                        
                        //只能base64  不然转义问题
                        let htmlCont=try? String(contentsOfFile: Bundle.main.path(forResource: "html/article/ArticleDetail", ofType: "html")!, encoding:String.Encoding.utf8)
                        let css=try? String(contentsOfFile: Bundle.main.path(forResource: "html/article/ArticleCss", ofType: "css")!, encoding:String.Encoding.utf8)
                        
                        xx = StringHelp.replaceAll(htmlCont, old: "#title#", new: data.Title ?? "");
                        
                        xx = StringHelp.replaceAll(xx, old: "#author#", new: data.Author ?? "");
                        
                        xx = StringHelp.replaceAll(xx, old: "#info#", new: "");
                        
                        xx = StringHelp.replaceAll(xx, old: "#time#", new: data.AddTime ?? "");
                        
                        xx = StringHelp.replaceAll(xx, old: "#content#", new: data.Content ?? "");
                        
                        xx  = xx.replacingOccurrences(of: "#css_include#", with: css!,  options: String.CompareOptions.literal, range: nil)
                        
                    } catch {
                        // deal with error
                    }
                    
                    DispatchQueue.main.async(execute: {
                        self.webView.loadHTMLString(xx, baseURL:URL(string: Api.getArticleBaseUrl()))
                        
                    })
                    
                })
                
                
                
                
            }else{
                ViewHelp.showTipsView(containView: self.view, tips: "加载失败")
            }
            ViewHelp.hideProgressActivity(containView: self.view)
            
        }
        taskAlls.append(ts)
    }
    
    
    var taskAlls = [Int]();
    deinit {
        //界面退出肯定就不登陆咯
        Api.destroyTask(taskSerialNumbers: taskAlls);
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
extension HelpDetailViewController: UIWebViewDelegate {
    
    func webViewDidStartLoad(_ webView: UIWebView) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        //        SVProgressHUD.dismiss()
        //这会等到图片全部加载完 里面的资源加载完
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        return true;
    }
}
